﻿#region Using

using Common.Base;
using Common.Enums;

using GameCommon.ComponentInterfaces;
using GameCommon.HitBoxes;
using GameCommon.ServiceInterfaces;

using GlobalServices = GameCommon.Services.Service;

#endregion

namespace GameCommon.Services
{
    internal class ComponentModifier : IComponentModifier
    {
        public void ApplyAlignment(Dimension componentDimension, Dimension parentDimension, Offset offset, HorizontalAlignment alignment)
        {
            switch (alignment)
            {
                case HorizontalAlignment.Left:
                    componentDimension.X = parentDimension.X + offset.Left;
                    break;
                case HorizontalAlignment.Center:
                    componentDimension.X = parentDimension.X + ((parentDimension.Width - componentDimension.Width) / 2);
                    break;
                case HorizontalAlignment.Right:
                    componentDimension.X = parentDimension.X + (parentDimension.Width - componentDimension.Width) - offset.Right;
                    break;
            }
        }

        public void ApplyAlignment(Dimension componentDimension, Dimension parentDimension, Offset offset, VerticalAlignment alignment)
        {
            switch (alignment)
            {
                case VerticalAlignment.Top:
                    componentDimension.Y = parentDimension.Y + offset.Top;
                    break;
                case VerticalAlignment.Middle:
                    componentDimension.Y = parentDimension.Y + ((parentDimension.Height - componentDimension.Height) / 2);
                    break;
                case VerticalAlignment.Bottom:
                    componentDimension.Y = parentDimension.Y + (parentDimension.Height - componentDimension.Height) - offset.Bottom;
                    break;
            }
        }

        public void ApplyAlignments(Dimension componentDimension, Dimension parentDimension, Offset offset, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
        {
            this.ApplyAlignment(componentDimension, parentDimension, offset, horizontalAlignment);
            this.ApplyAlignment(componentDimension, parentDimension, offset, verticalAlignment);
        }

        public RectangleHitBox GetTextHitBox(ITextHitBox text, string command)
        {
            var size = GetSize(text);
            return this.GetTextHitbox(text, size, command);
        }

        private RectangleHitBox GetTextHitbox(ITextHitBox textInput, Size size, string command)
        {
            return new RectangleHitBox(string.Format("RHB_{0}", textInput), new Dimension(textInput.Position, size), command);
        }

        private static Size GetSize(ITextHitBox text)
        {
            return GlobalServices.FontService.GetSize(text.Value, text.Font);
        }

    }
}